package com.shensi.adminCollect.service.station.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shensi.common.core.domain.model.LoginUser;
import com.shensi.common.core.utils.SecurityUtils;
import com.shensi.adminCollect.domain.station.AppUserSuperCopy;
import com.shensi.adminCollect.mapper.station.AppUserSuperCopyMapper;
import com.shensi.adminCollect.service.station.AppUserSuperCopyService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @date 2023/12/12
 */
@Service
public class AppUserSuperCopyServiceImpl extends ServiceImpl<AppUserSuperCopyMapper, AppUserSuperCopy> implements AppUserSuperCopyService {

    @Override
    @Transactional
    public void saveCopy(AppUserSuperCopy appUserSuperCopy) {
        AppUserSuperCopy one = getOneByUser();
        Integer enterpriseId = one.getEnterpriseId();
        if (enterpriseId == null) {
            return;
        }
        appUserSuperCopy.setId(one.getId());
        appUserSuperCopy.setEnterpriseId(one.getEnterpriseId());
        saveOrUpdate(appUserSuperCopy);
    }

    @Override
    public AppUserSuperCopy getOneByUser() {
        LoginUser user = SecurityUtils.getLoginUser();
        Long enterpriseId = user.getUser().getEnterpriseId();
        if (enterpriseId == null) {
            return new AppUserSuperCopy();
        }
        AppUserSuperCopy form = new AppUserSuperCopy();
        form.setEnterpriseId(Math.toIntExact(enterpriseId));
        QueryWrapper<AppUserSuperCopy> qw = new QueryWrapper<>(form);
        qw.last("limit 1");
        AppUserSuperCopy one = getOne(qw);
        return one == null ? form : one;
    }
}
